from toee import *
from utilities import *
from council import *

def san_dialog( attachee, triggerer ):
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		triggerer.begin_dialog( attachee, 210 )
	elif (attachee.map == 5001):
		attachee.float_line(600,triggerer)
	elif (game.global_flags[835] == 1 and game.global_flags[37] == 0 and game.global_flags[842] == 1 and game.global_flags[839] == 0):
		triggerer.begin_dialog(attachee,1000)
	elif (game.party[0].reputation_has( 27 ) == 1): 
		triggerer.begin_dialog( attachee, 11002 )
	else:
		triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT

def san_dying( attachee, triggerer ):
	game.global_flags[437] = 1
	if (attachee.leader_get() == OBJ_HANDLE_NULL and attachee.item_find(5009) == OBJ_HANDLE_NULL and game.global_flags[850] == 1):
		game.global_flags[850] = 0
		create_item_in_inventory( 5009, attachee )
	if (attachee.leader_get() == OBJ_HANDLE_NULL):
		game.global_vars[23] = game.global_vars[23] + 1
		if (game.global_vars[23] >= 2):
			game.party[0].reputation_add( 1 )
	else:
		game.global_vars[29] = game.global_vars[29] + 1
	return RUN_DEFAULT

def san_enter_combat( attachee, triggerer ):
	if (attachee.leader_get() == OBJ_HANDLE_NULL and attachee.item_find(5009) != OBJ_HANDLE_NULL and game.global_flags[850] == 0):
		attachee.item_find(5009).destroy()
		game.global_flags[850] = 1
	return RUN_DEFAULT

def san_first_heartbeat(attachee, triggerer):
	if ( (attachee.map == 5016 and (get_v(435) == 3 or get_v(435) == 4)) or game.global_flags[437] == 1):
		attachee.object_flag_set(OF_OFF)
	if (get_v(435) >= 5 and game.global_flags[437] == 0):
		attachee.object_flag_unset(OF_OFF)
	return RUN_DEFAULT


def san_new_map( attachee, triggerer ):
	if ( attachee.stat_level_get(stat_level_fighter) >= 8 ):
		game.leader.begin_dialog( attachee, 230 )
	return SKIP_DEFAULT

def delete_townhall_npcs(attachee):
	for npc in game.obj_list_vicinity(attachee.location,OLC_NPC):
		if (to_be_deleted(npc) == 1):
			npc.destroy()
	return

def to_be_deleted(npc):
	if (npc.name == 14371 or npc.name == 20186 or (npc.name >= 14004 and npc.name <= 14012) or (npc.name >=20000 and npc.name <=20007) or npc.name == 8054):
		return 1
	return 0

def q15():
	if game.quests[15].state == qs_accepted or game.quests[15].state == qs_mentioned:
		game.quests[15].state = qs_completed
		record_time_stamp(427)
	return

def proactivity(npc,line_no):
	for pc in game.party:
		 if (critter_is_unconscious(pc) != 1 and pc.type == obj_t_pc):
			pc.begin_dialog(npc,line_no)
	return

def switch_to_burne(pc, rufus):
	q15()
	burne = find_npc_near(rufus, 8054)
	if burne.leader_get() != OBJ_HANDLE_NULL:
		pc.follower_remove( burne )
	game.timevent_add(proactivity,(burne,690),50)
	return
